package org.glow.changgou.order.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.glow.changgou.order.dao.OrderItemMapper;
import org.glow.changgou.order.pojo.OrderConfig;
import org.glow.changgou.order.pojo.OrderItem;
import org.glow.changgou.order.service.OrderItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/****
 * @Author:glow
 * @Description:OrderItem业务层接口实现类
 *****/
@Service
public class OrderItemServiceImpl  extends ServiceImpl<OrderItemMapper,OrderItem> implements OrderItemService {

    @Resource
    private OrderItemMapper orderItemMapper;


    /**
     * OrderItem条件+分页查询
     * @param orderItem 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @Override
    public Page<OrderItem> findPage(OrderItem orderItem, int page, int size){
        Page<OrderItem> pageInfo = new Page<>(page,size);
        LambdaQueryWrapper<OrderItem> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper = getCondition(queryWrapper,orderItem);
        return orderItemMapper.selectPage(pageInfo,queryWrapper);
    }

    /**
     * OrderItem分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<OrderItem> findPage(int page, int size){
        Page<OrderItem> pageInfo = new Page<>(page,size);

        return orderItemMapper.selectPage(pageInfo,null);
    }

    /**
     * OrderItem条件查询
     * @param orderItem
     * @return
     */
    @Override
    public List<OrderItem> findList(OrderItem orderItem){

        LambdaQueryWrapper<OrderItem> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper = getCondition(queryWrapper,orderItem);
        return orderItemMapper.selectList(queryWrapper);
    }


    /**
     * OrderItem构建查询对象
     * @param orderItem
     * @return
     */
    public LambdaQueryWrapper<OrderItem> getCondition(LambdaQueryWrapper<OrderItem> queryWrapper, OrderItem orderItem){

        if(orderItem!=null){
            // ID
            if(!StringUtils.isEmpty(orderItem.getId())){
//                    criteria.andEqualTo("id",orderItem.getId());
                    queryWrapper.eq(OrderItem::getId,orderItem.getId());
            }
            // 1级分类
            if(!StringUtils.isEmpty(orderItem.getCategoryId1())){
//                    criteria.andEqualTo("categoryId1",orderItem.getCategoryId1());
                    queryWrapper.eq(OrderItem::getCategoryId1,orderItem.getCategoryId1());
            }
            // 2级分类
            if(!StringUtils.isEmpty(orderItem.getCategoryId2())){
//                    criteria.andEqualTo("categoryId2",orderItem.getCategoryId2());
                queryWrapper.eq(OrderItem::getCategoryId2,orderItem.getCategoryId2());
            }
            // 3级分类
            if(!StringUtils.isEmpty(orderItem.getCategoryId3())){
//                    criteria.andEqualTo("categoryId3",orderItem.getCategoryId3());
                queryWrapper.eq(OrderItem::getCategoryId3,orderItem.getCategoryId3());
            }
            // SPU_ID
            if(!StringUtils.isEmpty(orderItem.getSpuId())){
//                    criteria.andEqualTo("spuId",orderItem.getSpuId());
                    queryWrapper.eq(OrderItem::getSpuId,orderItem.getSpuId());
            }
            // SKU_ID
            if(!StringUtils.isEmpty(orderItem.getSkuId())){
//                    criteria.andEqualTo("skuId",orderItem.getSkuId());
                    queryWrapper.eq(OrderItem::getSkuId,orderItem.getSkuId());
            }
            // 订单ID
            if(!StringUtils.isEmpty(orderItem.getOrderId())){
//                    criteria.andEqualTo("orderId",orderItem.getOrderId());
                    queryWrapper.eq(OrderItem::getOrderId,orderItem.getOrderId());
            }
            // 商品名称
            if(!StringUtils.isEmpty(orderItem.getName())){
//                    criteria.andLike("name","%"+orderItem.getName()+"%");
                    queryWrapper.like(OrderItem::getName,orderItem.getName());
            }
            // 单价
            if(!StringUtils.isEmpty(orderItem.getPrice())){
//                    criteria.andEqualTo("price",orderItem.getPrice());
                    queryWrapper.eq(OrderItem::getPrice,orderItem.getPrice());
            }
            // 数量
            if(!StringUtils.isEmpty(orderItem.getNum())){
//                    criteria.andEqualTo("num",orderItem.getNum());
                    queryWrapper.eq(OrderItem::getNum,orderItem.getNum());
            }
            // 总金额
            if(!StringUtils.isEmpty(orderItem.getMoney())){
//                    criteria.andEqualTo("money",orderItem.getMoney());
                    queryWrapper.eq(OrderItem::getMoney,orderItem.getMoney());
            }
            // 实付金额
            if(!StringUtils.isEmpty(orderItem.getPayMoney())){
//                    criteria.andEqualTo("payMoney",orderItem.getPayMoney());
                    queryWrapper.eq(OrderItem::getPayMoney,orderItem.getPayMoney());
            }
            // 图片地址
            if(!StringUtils.isEmpty(orderItem.getImage())){
//                    criteria.andEqualTo("image",orderItem.getImage());
                    queryWrapper.eq(OrderItem::getImage,orderItem.getImage());
            }
            // 重量
            if(!StringUtils.isEmpty(orderItem.getWeight())){
//                    criteria.andEqualTo("weight",orderItem.getWeight());
                    queryWrapper.eq(OrderItem::getWeight,orderItem.getWeight());
            }
            // 运费
            if(!StringUtils.isEmpty(orderItem.getPostFee())){
//                    criteria.andEqualTo("postFee",orderItem.getPostFee());
                    queryWrapper.eq(OrderItem::getPostFee,orderItem.getPostFee());
            }
            // 是否退货,0:未退货，1：已退货
            if(!StringUtils.isEmpty(orderItem.getIsReturn())){
//                    criteria.andEqualTo("isReturn",orderItem.getIsReturn());
                    queryWrapper.eq(OrderItem::getIsReturn,orderItem.getIsReturn());
            }
        }
        return queryWrapper;
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(String id){
        orderItemMapper.deleteById(id);
    }

    /**
     * 修改OrderItem
     * @param orderItem
     */
    @Override
    public void update(OrderItem orderItem){
        orderItemMapper.updateById(orderItem);
    }

    /**
     * 增加OrderItem
     * @param orderItem
     */
    @Override
    public void add(OrderItem orderItem){
        orderItemMapper.insert(orderItem);
    }

    /**
     * 根据ID查询OrderItem
     * @param id
     * @return
     */
    @Override
    public OrderItem findById(String id){
        return  orderItemMapper.selectById(id);
    }

    /**
     * 查询OrderItem全部数据
     * @return
     */
    @Override
    public List<OrderItem> findAll() {
        return orderItemMapper.selectList(null);
    }
}
